package com.blog.controller;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

import com.blog.service.ArticleLikeService;
import com.blog.service.ArticleService;
import com.blog.service.UserService;
import com.blog.service.NotificationService;
import com.blog.websocket.NotificationWebSocket;
import com.blog.entity.Article;
import com.blog.entity.User;
import com.blog.entity.Notification;


@RestController
@RequestMapping("/article-like")
public class ArticleLikeController {

    @Autowired
    private ArticleLikeService articleLikeService;

    @Autowired
    private ArticleService articleService;

    @Autowired
    private UserService userService;

    @Autowired
    private NotificationService notificationService;

    @PostMapping("/{articleId}/like")
    public ResponseEntity<Map<String, Object>> likeArticle(@PathVariable Long articleId) {
        // 检查用户是否已登录
        if (!StpUtil.isLogin()) {
            return ResponseEntity.status(401).body(Map.of("message", "未登录"));
        }

        Long userId = StpUtil.getLoginIdAsLong();

        try {
            boolean isLiked = articleLikeService.likeArticle(articleId, userId);

            // 如果是点赞成功，发送通知
            if (isLiked) {
                Article article = articleService.getById(articleId);
                if (article != null && !article.getAuthorId().equals(userId)) {
                    // 获取当前用户信息
                    User currentUser = userService.getById(userId);

                    // 创建通知
                    Notification likeNotification = new Notification();
                    likeNotification.setUserId(article.getAuthorId()); // 文章作者
                    likeNotification.setInitiatorId(userId); // 当前点赞用户
                    likeNotification.setType("ARTICLE_LIKE");
                    likeNotification.setContent("赞了你的文章");
                    likeNotification.setArticleId(article.getId());
                    likeNotification.setStatus(0); // 未读
                    likeNotification.setCreateTime(LocalDateTime.now());

                    // 保存通知
                    notificationService.save(likeNotification);

                    // 通过WebSocket发送实时通知
                    JSONObject notificationData = new JSONObject();
                    notificationData.set("type", "ARTICLE_LIKE");
                    notificationData.set("message", currentUser.getNickname() + "赞了你的文章");
                    notificationData.set("notification", likeNotification);

                    NotificationWebSocket.sendNotification(article.getAuthorId(), notificationData);
                }
            }

            return ResponseEntity.ok(Map.of(
                "message", isLiked ? "点赞成功" : "取消点赞成功",
                "liked", isLiked
            ));
        } catch (Exception e) {
            return ResponseEntity.badRequest().body(Map.of("message", e.getMessage()));
        }
    }
}
